# -*- coding: utf-8 -*-

from datetime import datetime
from flask_app.extensions import db
from .user_md import UserMD
from flask_app.app_utils import get_uuid
from .author_md import AuthorMD
from .user_role_md import UserRoleMD

class UserAccountMD(db.Model):
    __tablename__ = "user_account_t"
    # 用户唯一标识user_id
    id = db.Column(db.String(80), primary_key=True)
    
    
    # 登录名
    login_name = db.Column(db.String(80), unique=True)
    # 密码
    pass_word = db.Column(db.String(80))

    user = db.relationship("UserMD", backref='user_account',uselist=False)

    register_date = db.Column(db.DateTime, default=datetime.now)

    update_date = db.Column(db.DateTime, default=datetime.now)

    role_list = db.relationship("UserRoleMD", backref='role_user_account')



    
    # def get_id(self):
    #     return self.__id


    def __init__(self,login_name,pass_word, user_info={}):
        self.id = get_uuid()
        self.login_name = login_name
        self.user = UserMD(self.id,user_info)
        self.pass_word = pass_word

    def save(self):
        self.update_date = datetime.now()
        self.user.save()
        db.session.add(self)
        db.session.commit()


    @staticmethod
    def get_user_account(user_account_id):
        return db.session.query(UserAccountMD).get(user_account_id)

    def get_user_resource(self):
        resource = []
        resource.append(AuthorMD.get_authored_resource(self.id))

        for id in self.role_list:
            resource.append(AuthorMD.get_authored_resource(id))

        return resource
